(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.category.cate.cateUpdateContinueController', createContinueController);
	createContinueController.$inject = [ '$scope', 'Constants', 'SYS_PARAMS', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'categoryService' ];
	function createContinueController($scope, Constants, SYS_PARAMS, modalService, gridService, layoutConfig, validatorConfig, authService, categoryService) {
		var ctrl = this, categoryInfo = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.limitCategory = {};
		ctrl.init = function() {
			var params = {
					categoryCode : categoryInfo.categoryCode
			};
			authService.updateContinue($scope.$stateParams.params).then(function(response) {
				ctrl.limitCategory = response.data.model || {};
				ctrl.limitCategory.taskId = response.data.task.taskId;
				if(ctrl.limitCategory.updatedLimitNode){
					var  updatedLimitNode = ctrl.limitCategory.updatedLimitNode;
					updatedLimitNode.taskId = response.data.task.taskId;
					updatedLimitNode.limitCatName = ctrl.limitCategory.limitCatName;
					if('C' == updatedLimitNode.dataStatus){
						$scope.go('cda.category.cate.nodeCreateContinue', updatedLimitNode);
					}
					else if('U' == updatedLimitNode.dataStatus){
						$scope.go('cda.category.cate.nodeUpdateContinue', updatedLimitNode);
					}
					else if('D' == updatedLimitNode.dataStatus){
						$scope.go('cda.category.cate.nodeDeleteVerify', updatedLimitNode);
					}
				}
			});
		};

		ctrl.updateContinueSave = function() {
			var messages = [];
			angular.forEach(errorMessage, function(field) {
				angular.forEach(field, function(rule) {
					messages.push(rule);
				});
			});
			if (messages.length > 0) {
				modalService.openErrorDialogs({
					'messages' : messages
				});
			} else {
				return authService.updateContinueSave(ctrl.limitCategory).then(saveSuccessful, saveFailed);
			}
		};

		ctrl.updateContinueSubmit = function() {
			var messages = [];
			angular.forEach(errorMessage, function(field) {
				angular.forEach(field, function(rule) {
					messages.push(rule);
				});
			});
			if (messages.length > 0) {
				modalService.openErrorDialogs({
					'messages' : messages
				});
			} else {
				return authService.updateContinueSubmit(ctrl.limitCategory).then(submitSuccessful, submitFailed);
			}
		};
		
			
		ctrl.updateContinueCancel = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.cancel', args : [ctrl.limitCategory.limitCatName]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.updateContinueCancel(ctrl.limitCategory).then(cancelSuccessful, cancelFailed);
					}
				}
			});
		};
		

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedLimitCategory = selectedRows;
		}

		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'Submit Successfully!' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.category.list',
					'state' : 'cda.category.cate',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
			if (response.data && response.data.model) {
				ctrl.limitCategory = response.data.model;
			}
			if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
		function cancelSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.cancel' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.category.list',
					'state' : 'cda.category.cate',
					'params' : {}
				}]
			});
		}

		function cancelFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}		
	}
})(angular);